package cn.lg.soar.common.exceptions;

import cn.lg.soar.common.data.msg.I18n;

import java.text.MessageFormat;

/**
 * 框架基础异常类，便于统一信息提示
 * @author luguoxiang
 * @date 2022/6/27
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class BaseException extends RuntimeException {

    private String code;

    private I18n msg;

    private Object[] msgArgs;

    public BaseException(String code, I18n message, Object... args){
        super(message.msg(args));
        this.code = code;
        this.msg = message;
        this.msgArgs = args;
    }

    public BaseException(String code, String message, Object... args){
        this(code, I18n.build(message), args);
    }

    public String getCode() {
        return code;
    }

    public I18n getMsg() {
        return msg;
    }

    public Object[] getMsgArgs() {
        return msgArgs;
    }

    /**
     * 检查异常码范围
     * @param code
     * @param min
     * @param max
     */
    protected void checkCodeRange(int code, int min, int max) {
        if (code < min || max < code) {
            throw new RuntimeException(MessageFormat.format("异常码不在范围：{0} ~ {1}", min, max));
        }
    }

}
